//
//  configRouterViewController.m
//  MMWMiotLinkDemo
//
//  Created by   HGG  on 16/1/12.
//  Copyright © 2016年 HGG. All rights reserved.
//

#import "configRouterViewController.h"
#import "MMWMiotLink.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import "firstConfigViewController.h"

@interface configRouterViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *wifiNameLB;
@property (weak, nonatomic) IBOutlet UITextField *wifiPassWordTF;
@property (weak, nonatomic) IBOutlet UITextField *configModeTF;

@property (weak, nonatomic) IBOutlet UITextField *timeOutTF;

@property (weak, nonatomic) IBOutlet UILabel *deviceInfoLB;


@end

@implementation configRouterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.wifiNameLB.text = [self getWifiInfo][@"SSID"];
   
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_wifiPassWordTF resignFirstResponder];
    [_configModeTF resignFirstResponder];
    [_timeOutTF resignFirstResponder];

}

- (IBAction)textFieldDidEndEditing:(id)sender {
    if (sender == _timeOutTF) {
        CGRect bounds = self.view.bounds;
        bounds.origin.y = 0.f;
        self.view.bounds = bounds;
    }
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == _timeOutTF) {
        CGRect bounds = self.view.bounds;
        bounds.origin.y = 100.f;
        self.view.bounds = bounds;
    }
    return YES;
}
- (IBAction)configRouterAction:(id)sender {
    //&& (![[[self getWifiInfo][@"SSID"] substringToIndex:10] isEqualToString:@"MiotLinkAp"])

    __weak typeof(self)weakSelf = self;
    [[MMWMiotLink shareInstence] mmw_MiotLinkStopLink];
    NSLog(@"wifiName==%@\nwifiPass==%@",self.wifiNameLB.text,self.wifiPassWordTF.text);
    [[MMWMiotLink shareInstence] mmw_MiotLinkConfig:self.wifiNameLB.text password:self.wifiPassWordTF.text configMode:4 time:[self.timeOutTF.text intValue] callBack:^(NSDictionary *deviceDic)
     {

         __strong typeof(weakSelf)strongSelf = weakSelf;
         NSLog(@"%@",deviceDic);
         firstConfigViewController *firstConfigVC= [firstConfigViewController new];
         MMWMiotLinkDeviceInfo *device = [[MMWMiotLinkDeviceInfo alloc]init];
         device.ip = deviceDic[@"response"][@"ip"];
         device.mac = deviceDic[@"response"][@"mac"];
         device.port = ((NSString *)deviceDic[@"response"][@"port"]).intValue;
         firstConfigVC.device = device;
         strongSelf.deviceInfoLB.text = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:deviceDic options:0 error:nil] encoding:NSUTF8StringEncoding];
//         NSLog(@"%@",_deviceInfoLB.text);
         if (device.port!=0) {
             [strongSelf.navigationController pushViewController:firstConfigVC animated:YES];
         }
         
     }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
//    [[MMWMiotLink shareInstence] mmw_MiotLinkStopLink];
}

- (BOOL)isWiFiOk
{
    NSString *wifiName = [self getWifiInfo][@"SSID"];
    return wifiName && [wifiName hasPrefix:@"MiotLinkAp"];
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSDictionary*)getWifiInfo{
    
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    
    if (!wifiInterfaces) {
        return nil;
    }
    
    NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
    
    for (NSString *interfaceName in interfaces) {
        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
        
        if (dictRef) {
            NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
            NSLog(@"network info -> %@", networkInfo);
            
            return networkInfo;
        }
    }
    
    return nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
